#Helper functions for US Conjoint

#Transform Group

transform_df <- function(picture_name1,picture_dis_name1,
                         picture_name2,picture_dis_name2,
                         var_cs,var_rs1,var_rs2,var_ca,var_rp1,var_rp2,
                         group_data){
  for (i in seq(nrow(group_data))){
    
    id <- group_data[i,"lfdn"]
    picture <- picture_name1
    picture_dis <- picture_dis_name1  
    chose_stronger <- group_data[i,var_cs]
    rating_stronger <- group_data[i,var_rs1]
    chose_appropiate <- group_data[i,var_ca]
    rating_protest <- group_data[i,var_rp1]
    anti <- group_data[i,"anti"]
    urban <- group_data[i,"urban"]
    media <-group_data[i,"news_almnever"]
    education <- group_data[i,"highed"]
    stance <- group_data[i,"v_109"]
    party <- group_data[i,"v_89"]
    ethnic_group_sig <- group_data[i,"ethnic_group_sig"]
    duration <- group_data[i,"duration"]
    profile <- 1
    
    #Create rating of first profile
    df1 <- c(id,picture,picture_dis,chose_stronger,rating_stronger,chose_appropiate,
             rating_protest,anti,urban,media,education,profile,stance,party,ethnic_group_sig,duration)
    
    id <- group_data[i,"lfdn"]
    picture <- picture_name2
    picture_dis <- picture_dis_name2  
    chose_stronger <- group_data[i,var_cs]
    rating_stronger <- group_data[i,var_rs2]
    chose_appropiate <- group_data[i,var_ca]
    rating_protest <- group_data[i,var_rp2]
    anti <- group_data[i,"anti"]
    urban <- group_data[i,"urban"]
    media <-group_data[i,"news_almnever"]
    education <- group_data[i,"highed"]
    stance <- group_data[i,"v_109"]
    party <- group_data[i,"v_89"]
    ethnic_group_sig <- group_data[i,"ethnic_group_sig"]
    duration <- group_data[i,"duration"]
    profile <- 2
    
    #Create rating of second profile
    df2 <- c(id,picture,picture_dis,chose_stronger,rating_stronger,chose_appropiate,
             rating_protest,anti,urban,media,education,profile,stance,party,ethnic_group_sig,duration)
    
    #Combine
    df <- as.data.frame(rbind(df1,df2))
    names <-c("id","picture","picture_dis","chose_stronger","rating_stronger","chose_appropiate",
              "rating_protest","anti","urban","media","education","profile","stance","party",
              "ethnic_group_sig","duration")
    
    names(df) <- names
    #Get rid of lists within data frames
    for (j in names){
      df[j] <- unlist(df[j])
    }
    
    df$chose_stronger <- ifelse((df$chose_stronger=="Image 1" & 
                                   df$profile==1) |
                                  (df$chose_stronger=="Image 2" & 
                                     df$profile==2),1,0)
    df$chose_appropiate <- ifelse((df$chose_appropiate=="Image 1" & 
                                     df$profile==1) |
                                    (df$chose_appropiate=="Image 2" & 
                                       df$profile==2),1,0)
    
    
    
    
    if (i==1){
      df_final <- df
    }
    else{
      df_final <- rbind(df_final,df)
    }
    
  }
  return(df_final)
}


#Retrieve values for plots
amce_plot <- function(acme_result1,panel_name,label_name,
                      name_group1,name_group2,name_group3){
  p1 <- as.data.frame(acme_result1[1])
  p1[,1] <- c(rep(name_group1,2))
  p1[,2]<- c("Bureaucrats","Police")
  names(p1) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2 <- as.data.frame(acme_result1[3])
  p2[,1] <- c(rep(name_group2,2))
  p2[,2]<- c("Bureaucrats","Police")
  names(p2) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
 
  p3 <- as.data.frame(acme_result1[4])
  p3[,1] <- c(rep(name_group3,2))
  p3[,2]<- c("Bureaucrats","Police")
  names(p3) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out1 <- rbind(p1,p2,p3)
  
  out1$Group <- factor(out1$Group,levels=c(name_group1,name_group2,name_group3))
  out1$Level <- factor(out1$Level,levels=c("Police","Bureaucrats"))
  #Plot Figure
  
  #Confidence intervals
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  plot_pos <- ggplot(out1, aes())  
  plot_pos <- plot_pos + geom_hline(yintercept = 0, colour = "black", lty = 2)
  plot_pos <- plot_pos + geom_linerange(aes(x = Level,  ymin = Estimate - `Std. Err`*interval2,
                                            ymax = Estimate + `Std. Err`*interval2),
                                        lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + geom_pointrange(aes(x = Level, y = Estimate, ymin = Estimate - `Std. Err`*interval2,
                                             ymax = Estimate + `Std. Err`*interval2),
                                         lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + coord_flip() + theme_bw() +theme(axis.text=element_text(size=12),
                                                          axis.title=element_text(size=12)) 
  plot_pos <- plot_pos + ggtitle(panel_name) + xlab("") + ylab("") + scale_size(guide = 'none') + 
    facet_wrap(~ Group, ncol=3, scales = "fixed") +
    ylab(label_name)+
    theme(strip.text = element_text(size=12),
          plot.title = element_text(color="black", size=14, face="bold"),
          legend.position="none",
          strip.text.x =  element_text(size=12),
          panel.spacing.x = unit(10, "mm"))+
    scale_y_continuous(
      labels = scales::number_format(accuracy = 0.01))
  return(plot_pos)
}


amce_plot_same <- function(acme_result1,diff_result,panel_name,label_name,
                      name_group2,name_group3,name_diff){

  p2 <- as.data.frame(acme_result1[3])
  p2[,1] <- c(rep(name_group2,2))
  p2[,2]<- c("Bureaucrats","Police")
  names(p2) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3 <- as.data.frame(acme_result1[4])
  p3[,1] <- c(rep(name_group3,2))
  p3[,2]<- c("Bureaucrats","Police")
  names(p3) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  
  diff_res <- broom::tidy(diff_result)
  diff_res  <- cbind(c(rep(name_diff,2)),c("Bureaucrats","Police"),diff_res[5:6,c(2:5)],"")
  names(diff_res) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  out1 <- rbind(p2,p3,diff_res)
  
  out1$Group <- factor(out1$Group,levels=c(name_group2,name_group3,name_diff))
  out1$Level <- factor(out1$Level,levels=c("Police","Bureaucrats"))
  #Plot Figure
  
  #Confidence intervals
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  plot_pos <- ggplot(out1, aes())  
  plot_pos <- plot_pos + geom_hline(yintercept = 0, colour = "black", lty = 2)
  plot_pos <- plot_pos + geom_linerange(aes(x = Level,  ymin = Estimate - `Std. Err`*interval2,
                                            ymax = Estimate + `Std. Err`*interval2),
                                        lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + geom_pointrange(aes(x = Level, y = Estimate, ymin = Estimate - `Std. Err`*interval2,
                                             ymax = Estimate + `Std. Err`*interval2),
                                         lwd = 1/2, position = position_dodge(width = 0.9)) 
    
  plot_pos <- plot_pos + coord_flip() + theme_bw() +theme(axis.text=element_text(size=12),
                                                          axis.title=element_text(size=12)) 
  plot_pos <- plot_pos + ggtitle(panel_name) + xlab("") + ylab("") + scale_size(guide = 'none') + 
    facet_wrap(~ Group, ncol=3, scales = "fixed") +
    ylab(label_name)+
    theme(strip.text = element_text(size=12),
          plot.title = element_text(color="black", size=14, face="bold"),
          legend.position="none",
          strip.text.x =  element_text(size=12),
          panel.spacing.x = unit(10, "mm"))+
    scale_y_continuous(
      labels = scales::number_format(accuracy = 0.01), breaks=seq(-0.6,0.6,0.3), limit = c(-0.6,0.6))
  return(plot_pos)
}


amce_plot_2 <- function(acme_result1,panel_name,label_name,
                      name_group1,name_group2,name_group3,name_group4){
  p1 <- as.data.frame(acme_result1[1])
  p1[,1] <- c(rep(name_group1,2))
  p1[,2]<- c("Bureaucrats","Police")
  names(p1) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2 <- as.data.frame(acme_result1[3])
  p2[,1] <- c(rep(name_group2,2))
  p2[,2]<- c("Bureaucrats","Police")
  names(p2) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3 <- as.data.frame(acme_result1[4])
  p3[,1] <- c(rep(name_group3,2))
  p3[,2]<- c("Bureaucrats","Police")
  names(p3) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
#  out1 <- rbind(p1,p2,p3)
  
  p4 <- as.data.frame(acme_result1[5])
  p4[,1] <- c(rep(name_group4,2))
  p4[,2]<- c("Bureaucrats","Police")
  names(p4) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out1 <- rbind(p2,p3,p4)
  
  out1$Group <- factor(out1$Group,levels=c(name_group2,name_group3,name_group4))
  out1$Level <- factor(out1$Level,levels=c("Police","Bureaucrats"))
  #Plot Figure
  
  #Remove first group
  
  #Confidence intervals
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  plot_pos <- ggplot(out1, aes())  
  plot_pos <- plot_pos + geom_hline(yintercept = 0, colour = "black", lty = 2)
  plot_pos <- plot_pos + geom_linerange(aes(x = Level,  ymin = Estimate - `Std. Err`*interval2,
                                            ymax = Estimate + `Std. Err`*interval2),
                                        lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + geom_pointrange(aes(x = Level, y = Estimate, ymin = Estimate - `Std. Err`*interval2,
                                             ymax = Estimate + `Std. Err`*interval2),
                                         lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + coord_flip() + theme_bw() +theme(axis.text=element_text(size=12),
                                                          axis.title=element_text(size=12)) 
  plot_pos <- plot_pos + ggtitle(panel_name) + xlab("") + ylab("") + scale_size(guide = 'none') + 
    facet_wrap(~ Group, ncol=3, scales = "fixed") +
    ylab(label_name)+
    theme(strip.text = element_text(size=12),
          #axis.text.x = element_text(size=10),
          plot.title = element_text(color="black", size=14, face="bold"),
          legend.position="none",
          strip.text.x =  element_text(size=12),
          panel.spacing.x = unit(10, "mm"))+
    scale_y_continuous(
      labels = scales::number_format(accuracy = 0.01), breaks=seq(-0.6,0.6,0.3), limit = c(-0.6,0.6))
  return(plot_pos)
}

amce_plot_agg <- function(acme_result1,diff_result,panel_name,label_name,
                      name_group2,name_group3,name_diff){

  p2 <- as.data.frame(acme_result1[3])
  p2[,1] <- name_group2
  p2[,2]<- "Propaganda"
  names(p2) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3 <- as.data.frame(acme_result1[4])
  p3[,1] <- name_group3
  p3[,2]<- "Propaganda"
  names(p3) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out1 <- rbind(p2,p3)
  
  diff_res <- broom::tidy(diff_result)
  diff_res  <- cbind(name_diff,"Propaganda",diff_res[4,c(2:5)],"")
  names(diff_res) <- c("Group","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  
  out1 <- rbind(p2,p3,diff_res)
  
  out1$Group <- factor(out1$Group,levels=c(name_group2,name_group3,name_diff))
  #Plot Figure
  
  #Confidence intervals
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  plot_pos <- ggplot(out1, aes())  
  plot_pos <- plot_pos + geom_hline(yintercept = 0, colour = "black", lty = 2)
  plot_pos <- plot_pos + geom_linerange(aes(x = Level,  ymin = Estimate - `Std. Err`*interval2,
                                            ymax = Estimate + `Std. Err`*interval2),
                                        lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + geom_pointrange(aes(x = Level, y = Estimate, ymin = Estimate - `Std. Err`*interval2,
                                             ymax = Estimate + `Std. Err`*interval2),
                                         lwd = 1/2, position = position_dodge(width = 0.9)) 
  
  plot_pos <- plot_pos + coord_flip() + theme_bw() +theme(axis.text=element_text(size=12),
                                                          axis.title=element_text(size=12)) 
  plot_pos <- plot_pos + ggtitle(panel_name) + xlab("") + ylab("") + scale_size(guide = 'none') + 
    facet_wrap(~ Group, ncol=3, scales = "fixed") +
    ylab(label_name)+
    theme(strip.text = element_text(size=12),
          plot.title = element_text(color="black", size=14, face="bold"),
          legend.position="none",
          strip.text.x =  element_text(size=12),
          panel.spacing.x = unit(10, "mm"))+
    scale_y_continuous(
      labels = scales::number_format(accuracy = 0.01), breaks=seq(-0.6,0.6,0.3), limit = c(-0.6,0.6))
  return(plot_pos)
}

#Return tables
amce_latex <- function (acme_result1,acme_result1b,acme_result2,acme_result3,
                        group_name1,group_name2,no_outcomes,file_name){
  p1_n <- as.data.frame(rbind(c("","Strength perception (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result1[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2_n <- as.data.frame(rbind(c("",paste0("Strength perception (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result1[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Strength perception (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result1[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out1 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3)
  out <- out1
  
  if (no_outcomes>1){
    
   p1_n <- as.data.frame(rbind(c("","Political strength (unconditional)",rep("",5))))
    names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    p1 <- as.data.frame(acme_result1b[1])
    p1[,3:6] <- round(p1[,3:6],2) 
    names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    
    
    p2_n <- as.data.frame(rbind(c("",paste0("Political strength (",group_name1,")"),rep("",5))))
    names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    p2 <- as.data.frame(acme_result1b[3])
    p2[,3:6] <- round(p2[,3:6],2) 
    names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    
    p3_n <- as.data.frame(rbind(c("",paste0("Political strength (",group_name2,")"),rep("",5))))
    names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    p3 <- as.data.frame(acme_result1b[4])
    p3[,3:6] <- round(p3[,3:6],2) 
    names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
    out1b <- rbind(p1_n,p1,p2_n,p2,p3_n,p3)  
    
    
  p1_n <- as.data.frame(rbind(c("","Appropriateness perception (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result2[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  

  p2_n <- as.data.frame(rbind(c("",paste0("Appropriateness perception (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result2[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Appropriateness perception (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result2[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out2 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3)
  
  p1_n <- as.data.frame(rbind(c("","Protest willingness (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result3[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2_n <- as.data.frame(rbind(c("",paste0("Protest willingness (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result3[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Protest willingness (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result3[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out3 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3)
  
  #Number of participants
  
  out <- rbind(out1,out1b,out2,out3)
  }
  out <- out[,-1] #Attribute not needed
  out <- out[,-6] #Stars not needed
  out <- xtable(out)
  return(print(out,include.rownames=FALSE,round=2,file=file_name))
}

amce_latex_2 <- function (acme_result1,acme_result2,acme_result3,
                          group_name1,group_name2,group_name3){
  p1_n <- as.data.frame(rbind(c("","Strength perception (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result1[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2_n <- as.data.frame(rbind(c("",paste0("Strength perception (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result1[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Strength perception (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result1[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p4_n <- as.data.frame(rbind(c("",paste0("Strength perception (",group_name3,")"),rep("",5))))
  names(p4_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p4 <- as.data.frame(acme_result1[5])
  p4[,3:6] <- round(p4[,3:6],2) 
  names(p4) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  out1 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3,p4_n,p4)
  
  
  p1_n <- as.data.frame(rbind(c("","Appropriateness perception (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result2[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  
  p2_n <- as.data.frame(rbind(c("",paste0("Appropriateness perception (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result2[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Appropriateness perception (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result2[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p4_n <- as.data.frame(rbind(c("",paste0("Appropriateness perception (",group_name3,")"),rep("",5))))
  names(p4_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p4 <- as.data.frame(acme_result2[5])
  p4[,3:6] <- round(p4[,3:6],2) 
  names(p4) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  out2 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3,p4_n,p4)
  
  p1_n <- as.data.frame(rbind(c("","Protest willingness (unconditional)",rep("",5))))
  names(p1_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p1 <- as.data.frame(acme_result3[1])
  p1[,3:6] <- round(p1[,3:6],2) 
  names(p1) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p2_n <- as.data.frame(rbind(c("",paste0("Protest willingness (",group_name1,")"),rep("",5))))
  names(p2_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p2 <- as.data.frame(acme_result3[3])
  p2[,3:6] <- round(p2[,3:6],2) 
  names(p2) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p3_n <- as.data.frame(rbind(c("",paste0("Protest willingness (",group_name2,")"),rep("",5))))
  names(p3_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p3 <- as.data.frame(acme_result3[4])
  p3[,3:6] <- round(p3[,3:6],2) 
  names(p3) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  p4_n <- as.data.frame(rbind(c("",paste0("Protest willingness (",group_name3,")"),rep("",5))))
  names(p4_n) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  p4 <- as.data.frame(acme_result3[5])
  p4[,3:6] <- round(p4[,3:6],2) 
  names(p4) <- c("Attribute","Level","Estimate","Std. Err","z value","Pr(>|z|)","")
  
  out3 <- rbind(p1_n,p1,p2_n,p2,p3_n,p3,p4_n,p4)
  
  #Number of participants
  
  out <- rbind(out1,out2,out3)
  out <- out[,-1] #Attribute not needed
  out <- out[,-6] #Stars not needed
  out <- xtable(out)
  return(print(out,include.rownames=FALSE,round=2))
}



#Transform Data for Replication Experiment

#Transform Group

transform_df_rep <- function(group,group_broad,
                         var_cs,var_rs,var_ca,var_rp,group_data){
  for (i in seq(nrow(group_data))){
    
    id <- unlist(group_data[i,"lfdn"])
    group <- group
    group_broad <- group_broad
    chose_stronger <- unlist(group_data[i,var_cs])
    rating_stronger <- unlist(group_data[i,var_rs])
    chose_appropiate <- unlist(group_data[i,var_ca])
    rating_protest <- unlist(group_data[i,var_rp])
    anti <- unlist(group_data[i,"anti"])
    urban <- unlist(group_data[i,"urban"])
    media <-unlist(group_data[i,"news_almnever"])
    education <- unlist(group_data[i,"highed"])
    stance <- unlist(group_data[i,"v_109"])
    party <- unlist(group_data[i,"v_89"])
    ethnic_group_sig <- unlist(group_data[i,"ethnic_group_sig"])
    blm_att <- unlist(group_data[i,"v_301"])
    blm_protest <- unlist(group_data[i,"v_302"])
    duration <- unlist(group_data[i,"duration"])

    #Create rating of first profile
    df <- data.frame(id,group,group_broad,chose_stronger,rating_stronger,chose_appropiate,
             rating_protest,anti,urban,media,education,stance,party,ethnic_group_sig,
             blm_att,blm_protest,duration)
    
    names <-c("id","group","group_broad","rating_stronger","rating_stronger_pol","rating_appropiate",
              "rating_protest","anti","urban","media","education","stance","party",
              "ethnic_group_sig","blm_att","blm_protest","duration")
    
    names(df) <- names
   
    if(i==1) {
      df_final <- df
    }
    else{
      df_final <- rbind(df_final,df)
    }
    
  }
  return(df_final)
}



marginal_plot <- function(model1,model2,model3,panel_name,label_name){
  
  p1 <- as.data.frame(summary(model1)[4])
  p1 <- p1[2:3,]
  names(p1) <- c("Estimate","Std. Err","z value","Pr(>|z|)")
  p1$Group <- "Full Sample"
  p1$Level <- c("Bureaucrats","Police")
  
  p2 <- as.data.frame(summary(model2)[4])
  p2 <- p2[2:3,]
  names(p2) <- c("Estimate","Std. Err","z value","Pr(>|z|)")
  p2$Group <- "Supporters"
  p2$Level <- c("Bureaucrats","Police")
  
  p3 <- as.data.frame(summary(model3)[4])
  p3 <- p3[2:3,]
  names(p3) <- c("Estimate","Std. Err","z value","Pr(>|z|)")
  p3$Group <- "Opponents"
  p3$Level <- c("Bureaucrats","Police")
  
  out1 <- rbind(p1,p2,p3)
  
  out1$Group <- factor(out1$Group,levels=c("Full Sample","Supporters","Opponents"))
  out1$Level <- factor(out1$Level,levels=c("Police","Bureaucrats"))
  #Plot Figure
  
  #Confidence intervals
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  plot_pos <- ggplot(out1, aes())  
  plot_pos <- plot_pos + geom_hline(yintercept = 0, colour = "black", lty = 2)
  plot_pos <- plot_pos + geom_linerange(aes(x = Level,  ymin = Estimate - `Std. Err`*interval2,
                                            ymax = Estimate + `Std. Err`*interval2),
                                        lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + geom_pointrange(aes(x = Level, y = Estimate, ymin = Estimate - `Std. Err`*interval2,
                                             ymax = Estimate + `Std. Err`*interval2),
                                         lwd = 1/2, position = position_dodge(width = 0.9))
  plot_pos <- plot_pos + coord_flip() + theme_bw() +theme(axis.text=element_text(size=12),
                                                          axis.title=element_text(size=12)) 
  plot_pos <- plot_pos + ggtitle(panel_name) + xlab("") + ylab("") + scale_size(guide = 'none') + 
    facet_wrap(~ Group, ncol=3, scales = "fixed") +
    ylab(label_name)+
    theme(strip.text = element_text(size=12),
          plot.title = element_text(color="black", size=14, face="bold"),
          legend.position="none",
          strip.text.x =  element_text(size=12),
          panel.spacing.x = unit(10, "mm"))+
    scale_y_continuous(
      labels = scales::number_format(accuracy = 0.01))
  return(plot_pos)
}



